package com.itggm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itggm.mapper.EmpMapper;
import com.itggm.pojo.Emp;
import com.itggm.pojo.PageBean;
import com.itggm.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Override
    public Emp login(Emp emp) {
        //调用dao层功能：登录
        Emp loginEmp = empMapper.getByUsernameAndPassword(emp);
        //返回查询结果给Controller
        return loginEmp;
    }

    @Autowired
    private EmpMapper empMapper;
    @Transactional
    @Override
    public void save(Emp emp) {
        //补全数据
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //调用持久层增加功能
        empMapper.insert(emp);
    }
   /* @Override
    public PageBean page(Integer page, Integer pageSize) {
        Long count = empMapper.count();
        int start = (page-1)*pageSize;
        List<Emp> list = empMapper.list(start, pageSize);
        PageBean pageBean = new PageBean(count, list);
        return pageBean;
    }*/
    //使用pageHelper分页查询
   @Transactional
   @Override
   public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
       //设置分页参数
       PageHelper.startPage(page, pageSize);
       //执行条件分页查询
       List<Emp> empList = empMapper.list(name, gender, begin, end);
       //获取查询结果
       Page<Emp> p = (Page<Emp>) empList;
       //封装PageBean
       PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
       return pageBean;
   }
   // 批量删除
    @Transactional
    @Override
    public void delete(List<Integer> ids) {
       empMapper.delete(ids);
   }
    @Transactional
    @Override
    public Emp seleteByID(Integer id) {
        return empMapper.getByID(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now()); //更新修改时间为当前时间
        empMapper.update(emp);
    }
}
